package com.intelligent.ispc.foundation.chart.axis;

import org.jfree.chart.axis.NumberAxis;
import org.jfree.data.Range;

/**
 * Created by Can.Guan on 2016/7/13.
 * Modified by QiangChen on 2016/11/7.
 */
public class FixedNumberAxis extends NumberAxis {
    private Range r;

    public FixedNumberAxis() {
        super();
    }

    public FixedNumberAxis(String label) {
        super(label);
    }

    public Range getR() {
        return r;
    }

    public void setR(Range r) {
        this.r = r;
    }

    /*
     * Override the autoAdjustRange function to control the Y coordinate range
     */
    @Override
    protected void autoAdjustRange() {
        setRange(r);
    }

    /**
     * Override zoomRange function to achieve image magnification control
    */
    @Override
    public void zoomRange(double lowerPercent, double upperPercent) {

        double maxMagnification = 1000.0;
        double start = getRange().getLowerBound();
        double length = getRange().getLength();
        double r0;
        double r1;

        if (length < (getR().getUpperBound() - getR().getLowerBound()) / maxMagnification) {

            r0 = start;
            r1 = start + length;

            if (r1 > r0 && !java.lang.Double.isInfinite(r1 - r0)) {
                this.setRange(new Range(r0, r1));
            }

        } else {

            if (this.isInverted()) {
                r0 = start + length * (1.0D - upperPercent);
                r1 = start + length * (1.0D - lowerPercent);
            } else {
                r0 = start + length * lowerPercent;
                r1 = start + length * upperPercent;
            }

            if (r1 > r0 && !java.lang.Double.isInfinite(r1 - r0)) {
                this.setRange(new Range(r0, r1));
            }
        }
    }
}
